package com.example.spring.spring.annotation.spring;

import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;

/**
 * @author: orange
 * @create: 2023-08-12 15:37
 * @description:
 */
public class MergedAnnotationMain implements InterfaceMergedAnnotation  {

    public static void main(String[] args) {
        annotatedElementUtils();
    }


    public static void mergedAnnotationUser() {
        MergedAnnotations mergedAnnotations = MergedAnnotations.from(MergedAnnotationMain.class,
                MergedAnnotations.SearchStrategy.TYPE_HIERARCHY, RepeatableContainers.none());
        MergedAnnotation<RestController> mergedAnnotation = mergedAnnotations.get(RestController.class);
        System.out.println(mergedAnnotation.getString("value"));
        System.out.println(mergedAnnotations.isPresent(Controller.class));
        System.out.println(mergedAnnotation.asAnnotationAttributes());

        MergedAnnotation<Profile> profile = mergedAnnotations.get(Profile.class);
        System.out.println(profile.asAnnotationAttributes(MergedAnnotation.Adapt.values(false, true)));
    }

    public static void annotatedElementUtils() {
        // 工具类使用
        System.out.println(AnnotatedElementUtils.findMergedAnnotationAttributes(MergedAnnotationMain.class, AliasOrange.class, true, true));
    }

    public static void annotatedMethod() {
        Method[] methods = AliasOrange.class.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println(methods[i].getParameterCount() + " " + methods[i].getReturnType());
        }
    }

    public static void attributesForRepeatable() {
        MergedAnnotations mergedAnnotations = MergedAnnotations.from(MergedAnnotationMain.class,
                MergedAnnotations.SearchStrategy.TYPE_HIERARCHY, RepeatableContainers.none());
//        AnnotationConfigUtils.attributesForRepeatable(mergedAnnotations, PropertySource.class, PropertySources.class);
    }
}
